﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;

namespace myWallet.App_Code
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (Request.QueryString["lang"] != null && !String.IsNullOrEmpty(Request.QueryString["lang"]))
            {
                LanguageHelper.SetLanguage(Request.QueryString["lang"].ToString());
                SetUICulture(Session["lang"].ToString());
            }
            else if (Request.Cookies["lang"] != null)
            {
                Session["lang"] = Request.Cookies["lang"].Value;
                SetUICulture(Session["lang"].ToString());
            }
            else if (Session["lang"] != null)
            {
                SetUICulture(Session["lang"].ToString());
            }
            else
            {
                LanguageHelper.SetLanguage();
                SetUICulture(Session["lang"].ToString());
            }
        }

        protected void SetUICulture(string lang)
        {
            try
            {
                Page.Culture = lang;
                Page.UICulture = lang;

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            }
            catch (Exception)
            {
                base.InitializeCulture();
                throw;
            }
        }
    }
}